package com.itheima.abbitmq.config;


import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitmqConfig {
    private static final String EXCHANGE_NAME = "topic_exchange_springboot";
    private static final String EXCHANGE_NAME_ROUTING = "routing_exchange_springboot";
    private static final String QUEUE_NAME = "topic_queue_springboot";
    private static final String QUEUE_NAME_ROUTING1 = "routing_queue_springboot1";
    private static final String QUEUE_NAME_ROUTING2 = "routing_queue_springboot2";

    @Bean("bootexchange")
    public Exchange bootExchange() {
        return ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable(true).build();
    }

    @Bean("bootexchangerouting")
    public Exchange bootExchangeRouting() {
        return ExchangeBuilder.directExchange(EXCHANGE_NAME_ROUTING).durable(true).build();
    }

    @Bean("bootQueue")
    public Queue bootQueue() {
        return QueueBuilder.durable(QUEUE_NAME).build();
    }

    @Bean("routing_queue_springboot1")
    public Queue bootQueueRouting() {
        return QueueBuilder.durable(QUEUE_NAME_ROUTING1).build();
    }

    @Bean("routing_queue_springboot2")
    public Queue bootQueueRouting2() {
        return QueueBuilder.durable(QUEUE_NAME_ROUTING2).withArgument("x-message-ttl",100000).build();
    }

    @Bean
    public Binding bindQueueExchange(@Qualifier("routing_queue_springboot1") Queue queue, @Qualifier("bootexchangerouting") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("error").noargs();
    }

    @Bean
    public Binding bindRoutingExchange2(@Qualifier("routing_queue_springboot2") Queue queue, @Qualifier("bootexchangerouting") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("info").noargs();
    }

    @Bean
    public Binding bindRoutingExchange(@Qualifier("bootQueue") Queue queue, @Qualifier("bootexchange") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("order.#").noargs();
    }
    //----------------------------------------------------------------------
    private static final String testQueueDlx="testQueueDlx";
    private static final String testExchangeDlx="testExchangeDlx";
    private static final String queueDlx="queueDlx";
    private static final String exchangeDlx="exchangeDlx";
    //正常队列
    @Bean("testQueueDlx")
    public Queue testQueueDlx(){
        return QueueBuilder.durable(testQueueDlx).withArgument("x-dead-letter-exchange","exchangeDlx")
                .withArgument("x-dead-letter-routing-key","dlx.hebe")
                .withArgument("x-message-ttl",10000)
                .withArgument("x-max-length",10).build();
    }
    //正常交换机
    @Bean("testExchangeDlx")
    public Exchange testExchangeDlx(){
        return ExchangeBuilder.topicExchange(testExchangeDlx)
                .durable(true).build();
    }
    @Bean
    public Binding bingTopicTestExchangeDlx(@Qualifier("testQueueDlx") Queue queue, @Qualifier("testExchangeDlx") Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("test.dlx.#").noargs();
    }
    //死信队列
    @Bean("queueDlx")
    public Queue QueueDlx(){
        return QueueBuilder.durable(queueDlx).build();
    }
    //死信交换机
    @Bean("exchangeDlx")
    public Exchange exchangeDlx(){
        return ExchangeBuilder.topicExchange(exchangeDlx).durable(true).build();
    }

    @Bean
    public Binding bingTopicExchangeDlx(@Qualifier("queueDlx") Queue queue, @Qualifier("exchangeDlx") Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("dlx.#").noargs();
    }
}
